package com.recruit.wechat.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.*;

import java.util.ArrayList;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import java.util.List;

@Configuration
@Slf4j
public class InterceptorContfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")

                .allowedOrigins("*")

                .allowCredentials(true)

                .allowedMethods("GET", "POST", "DELETE", "PUT")

                .maxAge(3600);

    }


    @Bean
    public JwtTokenInterceptor getJwtTokenInterceptor(){
        log.info("注入JwtTokenInterceptor");
        return new JwtTokenInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 创建拦截器对象
        HandlerInterceptor interceptor
                = getJwtTokenInterceptor();

        // 不拦截的路径(白名单，不需要登录就可以访问的资源)
        List<String> patterns = new ArrayList<>();
        patterns.add("/error/**");
        patterns.add("/static/win10/**");

        patterns.add("/WeChatUser/wechatUser/web/login");
        patterns.add("/WeChatUser/wechatUser/web/regUser");
        patterns.add("/WeChatUser/wechatUser/web/loginApi/**");

//        patterns.add("/login.html");
//        patterns.add("/WeChatUser/wechatUser/web/getId");


        // 添加注册拦截器
        registry.addInterceptor(interceptor)
                .addPathPatterns("/*/*/web/**")

                .excludePathPatterns(patterns);
    }


}
